<template>
  <div class="user-container">
    <div class="avatar">
      <el-avatar :src="userAcatar" :size="40" />
    </div>

    <div class="username-box">
      <div class="username">{{ username }}</div>
      <el-dropdown trigger="click">
        <div class="jobname">
          <span> {{ firstJobName }}</span>
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item v-for="item in jobList" :key="item.id" @click="switchPositions(item)">
              {{ item.department.departmentName }}/{{ item.jobName }}
            </el-dropdown-item>
            <!-- <el-dropdown-item @click="openDialog('infoRef')">
              <el-icon>
                <User />
              </el-icon>{{ $t("header.personalData") }}
            </el-dropdown-item>
            <el-dropdown-item @click="openDialog('passwordRef')">
              <el-icon>
                <Edit />
              </el-icon>{{ $t("header.changePassword") }}
            </el-dropdown-item> -->
            <el-dropdown-item divided @click="logout">
              <el-icon>
                <SwitchButton />
              </el-icon>{{ $t("header.logout") }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <InfoDialog ref="infoRef" />
    <PasswordDialog ref="passwordRef" />
  </div>

</template>

<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { LOGIN_URL, HOME_URL } from "@/config";
import { useRoute, useRouter } from "vue-router";
import { logoutApi } from "@/api/modules/login";
import { useUserStore } from "@/stores/modules/user";
import { useAuthStore } from "@/stores/modules/auth";
import { ElMessageBox, ElMessage } from "element-plus";
import InfoDialog from "./InfoDialog.vue";
import PasswordDialog from "./PasswordDialog.vue";
import { useTabsStore } from "@/stores/modules/tabs";

const router = useRouter();
// 用户信息
const userStore = useUserStore();
const username = computed(() => userStore.userInfo.username);
const userAcatar = computed(() => userStore.userInfo.personalPhotos);
const jobList = computed(() => userStore.sysJobList);
const firstJobName = computed(() => userStore.firstJobName);

// 权限
const authStore = useAuthStore();
const tabStore = useTabsStore();
const route = useRoute();

// 切换职位
const switchPositions = async (item: any) => {
  userStore.setFirstJobName(item.jobName)
  // userStore.setFirstJobId(item.id);

  // 2.重新获取菜单权限
  authStore.setCurrentJobId(item.id);
  const newMenuList = await authStore.getAuthMenuList();
  userStore.setAuthMenuList(newMenuList)
  router.replace(HOME_URL);
  // tabStore.closeMultipleTab(route.fullPath);
  tabStore.closeMultipleTab();
};


// 退出登录
const logout = () => {
  ElMessageBox.confirm("您是否确认退出登录?", "温馨提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(async () => {
    // 1.执行退出登录接口
    await logoutApi();

    // 2.清除 Token
    userStore.setToken("");

    // 3.重定向到登陆页
    router.replace(LOGIN_URL);
    ElMessage.success("退出登录成功！");
  });
};

// 打开修改密码和个人信息弹窗
const infoRef = ref<InstanceType<typeof InfoDialog> | null>(null);
const passwordRef = ref<InstanceType<typeof PasswordDialog> | null>(null);
const openDialog = (ref: string) => {
  if (ref == "infoRef") infoRef.value?.openDialog();
  if (ref == "passwordRef") passwordRef.value?.openDialog();
};


</script>

<style scoped lang="scss">
.user-container {
  min-width: 7vw;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0 0 0 0.5vw;
}

.username-box {
  width: calc(100% - 40px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  margin-left: 0.8vw;

  .username {
    font-size: 0.8rem;
    margin-bottom: 0.5rem;
  }

  .jobname {
    display: flex;
    font-size: 0.8rem;
  }
}

.avatar {
  width: 40px;
  height: 40px;
}
</style>
